package com.apes.scm.masterdata.party.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ChangeCompany.CONST_TABLE_NAME)
public class ChangeCompany extends Model {
    public static final String CONST_TABLE_NAME = "mas_change_company";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "修改公司功能")
    @Comment("编码")
    private String id;

    @Column(name = "original_company_id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "当事人")
    @Comment("原公司编号")
    private long originalCompanyId;   //当事人编码

    @Column(name = "original_company_name")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "当事人")
    @Comment("原公司名称")
    private String originalCompanyName;   //当事人编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("原公司")
    private PartyRole originalCompany; //当事人名称

    @Name
    @Column(name = "new_company", nullable = false, length = 120)
    @Comment("新公司名称")
    private String newCompany;

    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;

    @States(name = "状态", values = {
            @State(id = "sent", name = "创建"),
            @State(id = "approve", name = "已审核"),
            @State(id = "delete",name="删除")})
    @Column(name = "state")
    @Comment("状态")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUID; //删除

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate; //删除日期

    @Comment("审核人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    private PartyRole approveId;


    @Comment("审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    private Date approveDate;


    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
